<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>首页</h1>
    <% if(username){ %>
        <span><%=username%></span>
        <a href="/center">个人中心</a>
        <a class="logout" href="javascript:;">退出登录</a>
        <script>
            let logout = document.querySelector('.logout');
            logout.onclick =function(){
                let x =new XMLHttpRequest();
                x.open('GET','/logout');
                x.send();
                x.responseType = 'json';
                x.onreadystatechange = function(){
                    if(x.readyState === 4 && x.status === 200){
                        // json 
                        // console.log(x.response);
                        if(x.response.code == 200){
                            alert(x.response.message);
                            setTimeout(() => {
                                window.location.href = '/';
                            }, 1000);
                        }
                    }
                }
            }
        </script>
    <% }else{ %>
        <a href="/login">登录</a>
    <%}%>
    
</body>
</html>